import { ProProvider } from '@ant-design/pro-components';
import React, { PropsWithChildren } from 'react';
import FieldSfwAliyunOSSUpload from '../field/FieldSfwAliyunOSSUpload';
import FieldSfwAmapLocationSelect from '../field/FieldSfwAmapLocationSelect';
import FieldSfwAutoComplete from '../field/FieldSfwAutoComplete';
import FieldSfwCodeEditor from '../field/FieldSfwCodeEditor';
import FieldSfwModel from '../field/FieldSfwModel';

export type DynamicComponentProProviderProps = PropsWithChildren;

const DynamicComponentProProvider: React.FC<
  DynamicComponentProProviderProps
> = (props) => {
  const { children } = props;

  const values = React.useContext(ProProvider);

  return (
    <ProProvider.Provider
      value={{
        ...values,
        valueTypeMap: {
          sfwModel: {
            render: (_, props) => <FieldSfwModel {...props} />,
            renderFormItem: (_, props) => <FieldSfwModel {...props} />,
          },
          sfwAutoComplete: {
            render: (_, props) => <FieldSfwAutoComplete {...(props as any)} />,
            renderFormItem: (_, props) => (
              <FieldSfwAutoComplete {...(props as any)} />
            ),
          },
          sfwCode: {
            render: (_, props) => <FieldSfwCodeEditor {...props} />,
            renderFormItem: (_, props) => <FieldSfwCodeEditor {...props} />,
          },
          sfwUpload: {
            render: (_, props) => <FieldSfwAliyunOSSUpload {...props} />,
            renderFormItem: (_, props) => (
              <FieldSfwAliyunOSSUpload {...props} />
            ),
          },
          sfwMapLocation: {
            render: (_, props) => <FieldSfwAmapLocationSelect {...props} />,
            renderFormItem: (_, props) => (
              <FieldSfwAmapLocationSelect {...props} />
            ),
          },
        },
      }}
    >
      {children}
    </ProProvider.Provider>
  );
};

export default DynamicComponentProProvider;
